package com.bxx.controller;

import cn.hutool.core.bean.BeanUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bxx.common.dto.CommentDto;
import com.bxx.common.lang.Result;
import com.bxx.entity.Comment;
import com.bxx.service.CommentService;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

@RestController
public class CommentController {


    @Autowired
    CommentService commentService;

    /**
     * 未审核评论列表（不能再博客下面看见）
     * @param currentPage
     * @param pageSize
     * @return
     */
    @GetMapping("/comment/unReviewed")
    @RequiresAuthentication
    public Result unReviewedList(@RequestParam(defaultValue = "1") Integer currentPage, Integer pageSize){
        Page page = new Page(currentPage, pageSize);
        IPage pageData = commentService.page(page, new QueryWrapper<Comment>().orderByDesc("created").eq("status",0));
        return Result.succ(pageData);
    }




    @GetMapping("/comment/list")
    public Result list(@RequestParam(defaultValue = "1") Integer currentPage, @RequestParam(defaultValue = "20") Integer pageSize, @RequestParam Integer blogId){
//        Page page = new Page(currentPage, pageSize);
//        IPage pageData = commentService.page(page, new QueryWrapper<Comment>().orderByDesc("created").eq("status",1).eq("blog_id", blogId));
        Page<CommentDto> pageData = commentService.getCommentInfo(new Page<>(currentPage,pageSize), blogId);
        return Result.succ(pageData);
    }

    /**
     * 审核
     * @param data
     * @return
     */
    @PostMapping("/comment/review")
    @RequiresAuthentication
    public Result review(@Validated @RequestBody String data){
        JSONObject jsonObject = JSON.parseObject(data);
        Integer id = (Integer)jsonObject.get("id");
        Comment temp = commentService.getById(id);
        temp.setStatus(1);
        commentService.updateById(temp);
        return Result.succ(null);
    }

    /**
     * 禁用
     * @param data
     * @return
     */
    @PostMapping("/comment/disable")
    @RequiresAuthentication
    public Result disable(@Validated @RequestBody String data){
        JSONObject jsonObject = JSON.parseObject(data);
        Integer id = (Integer)jsonObject.get("id");
        Comment temp = commentService.getById(id);
        temp.setStatus(0);
        commentService.updateById(temp);
        return Result.succ(null);
    }

    /**
     * 根据ID删除评论
     * @param data
     * @return
     */
    @PostMapping("/comment/delete")
    public Result delete(@Validated @RequestBody String data){
        JSONObject jsonObject = JSON.parseObject(data);
        Integer id = (Integer)jsonObject.get("id");
        commentService.removeById(id);
        return Result.succ(null);
    }

    /**
     * 评论
     * @param comment
     * @return
     */
    @PostMapping("/comment/add")
    public Result add(@Validated @RequestBody Comment comment){
        Comment temp = new Comment();
        temp.setCreated(LocalDateTime.now());
        temp.setStatus(0);
        BeanUtil.copyProperties(comment,temp,"status","created","id");
        commentService.save(temp);
        return Result.succ(null);
    }
}
